#pragma once
#include<string>
#include"Protocol.hpp"
enum{
    DIV_ERR
};
class Servercal
{
    private:
    public:
    Response CalculatorHelper(const Request& rq)
    {
        Response rep(0,0);
        switch(rq.op_)
        {
            case '+':rep.result_=rq.x_+rq.y_;
            break;
            case '-':rep.result_=rq.x_-rq.y_;
            break;
            case '*':rep.result_=rq.x_*rq.y_;
            break;
            case '/':if(rq.y_==0){rep.code_=DIV_ERR;}else rep.result_=rq.x_/rq.y_;
            break;
        }
        return rep;
    }
    std::string Calculator( std::string &package)
    {
        std::string content;
       bool r= Decode(package,&content); //"100 + 10"  解包
       if(!r)return "";
        Request rq;
        rq.Deserialize(content); //反序列化
       Response sp= CalculatorHelper(rq); //获取结果
        content="";
        sp.Serialize(&content);//序列化
        content=Encode(content);//封装
        return content;
    }
};